package com.sky.service.user.impl;


import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.mapper.admin.DishMapper;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.mapper.user.ShoppingCartMpaaer;
import com.sky.pojo.Dish;
import com.sky.pojo.Setmeal;
import com.sky.pojo.ShoppingCart;
import com.sky.service.user.ShoppingCartSrevice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

//购物车增删改查菜品功能
@Service
@Slf4j
public class ShoppingCartSreviceImpl implements ShoppingCartSrevice {

    @Autowired
    private ShoppingCartMpaaer shoppingCartMpaaer;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;





    /**
     * 添加购物车
     *
     * @param dto
     */
    @Override
    public void insertShoppingCart(ShoppingCartDTO dto) {



        ShoppingCart shoppingCart=new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCart.setCreateTime(LocalDateTime.now());

        if (dto.getDishId()!=null){

            Dish dish = dishMapper.userSelectDish(dto.getDishId());
            shoppingCart.setDishFlavor(dto.getDishFlavor());

            BeanUtils.copyProperties(dish,shoppingCart);
            shoppingCart.setAmount(dish.getPrice());
            shoppingCart.setDishId(dish.getId());
            shoppingCart.setSetmealId(null);


            ShoppingCart one = shoppingCartMpaaer.selectOne(dto.getDishId(), null,dto.getDishFlavor());

            if (one!=null){
                Integer number = one.getNumber();
                number++;

                shoppingCartMpaaer.updateNumber(number, Math.toIntExact(one.getId()));



            }else {
                shoppingCart.setNumber(1);
                shoppingCartMpaaer.insertShoppingCart(shoppingCart);
            }



        }

        if (dto.getSetmealId()!=null){

            Setmeal setmeal = setmealMapper.selectSetmealId(Math.toIntExact(dto.getSetmealId()));

            BeanUtils.copyProperties(setmeal,shoppingCart);
            shoppingCart.setAmount(setmeal.getPrice());
            shoppingCart.setDishId(null);
            shoppingCart.setSetmealId(setmeal.getId());

            ShoppingCart one = shoppingCartMpaaer.selectOne(null, dto.getSetmealId(),dto.getDishFlavor());


            if (one!=null){
                Integer number = one.getNumber();
                number++;

                shoppingCartMpaaer.updateNumber(number, Math.toIntExact(one.getId()));
            }else {
                shoppingCart.setNumber(1);
                shoppingCartMpaaer.insertShoppingCart(shoppingCart);
            }

        }
    }


    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {

        return shoppingCartMpaaer.list();
    }

    /**
     * 删除购物车
     */
    @Override
    public void clean() {
        shoppingCartMpaaer.clean();
    }
}
